//
//  StatusCell.m
//  Guazi
//
//  Created by koolearn on 11-8-5.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "StatusCell.h"
#import "ImageStore.h"
#import "TweetImageStore.h"
#import "GuaziAppDelegate.h"

#define kLeft 10
#define kTop 10
#define kTextFontsize 13

@interface StatusCell (private)
- (CGFloat) getTextHeight ;
@end

@implementation StatusCell

@synthesize status;


- (id)initWithStatus:(Status*)astatus style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.status = astatus;
        User* user = status.user;
        
        CGFloat y = kTop;
        photoButton = [[UIButton alloc] initWithFrame:CGRectMake(kLeft, y, 50, 50)];
        ImageStore* imageStore = [GuaziAppDelegate getAppDelegate].imageStore;
        UIImage* image = [imageStore getProfileImage:user.profileImageUrl_s isLarge:NO delegate:self];
        [photoButton setImage:image forState:UIControlStateNormal];
        [self.contentView addSubview:photoButton];
        
        screenNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, y, 240, 20)];
        screenNameLabel.text = status.user.screenName;
        screenNameLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:screenNameLabel];
        y += 20;
        
        UIFont* textFont = [UIFont systemFontOfSize:13];
        CGFloat textHeight = [self getTextHeight];
        textLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, y, 240, textHeight)];
        textLabel.font = textFont;
        textLabel.text = status.text;
        [self.contentView addSubview:textLabel];
        y += textHeight;
        
        footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, y, 240, 20)];
        footerLabel.font = [UIFont systemFontOfSize:12];
        footerLabel.textColor = [UIColor colorWithRed:0.725f green:0.725f blue:0.725f alpha:1.0f];
        footerLabel.text = [NSString stringWithFormat:@"%@ %@", status.timestamp, status.source];
        [self.contentView addSubview:footerLabel];
        y+= 20;
        
        [self setNeedsLayout];
        [self setNeedsDisplay];
   }
    return self;
}


- (void) layoutSubviews {
    CGFloat y = kTop;
    photoButton.frame = CGRectMake(kLeft, y, 50, 50);
    screenNameLabel.frame = CGRectMake(70, y, 240, 20);
    y += 20;
    
    CGFloat textHeight = [self getTextHeight];
    textLabel.frame = CGRectMake(70, y, 240, textHeight);
    y += textHeight;
    
    footerLabel.frame = CGRectMake(70, y, 240, 20);
    [super layoutSubviews];
}


- (CGFloat) getTextHeight 
{
    UIFont* textFont = [UIFont systemFontOfSize:kTextFontsize];
    CGSize size = [status.text sizeWithFont:textFont constrainedToSize:CGSizeMake(240, 900) lineBreakMode:UILineBreakModeCharacterWrap];
    return size.height;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}


- (void)dealloc
{
    [photoButton release];
    [screenNameLabel release];
    [textLabel release];
    [footerLabel release];
    [super dealloc];
}

@end
